package com.softer.chengxin.project.dao.provider;

import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.project.domin.Housing;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;

/**
 * @author Gong Zhiyang
 * @date 2018/10/16
 */
public class HousingDaoProvider {
    /**
     * 查询物业用房数量
     *
     * @param map 查询实体
     * @return 数量
     */
    public String queryHousingNum(HashMap<String, Object> map) {
        StringBuffer sql = new StringBuffer(20);
        Housing housing = (Housing) map.get("housing");
        sql.append("SELECT COUNT(*) FROM company_housing WHERE project_id=#{housing.projectId}");
        commonSql(sql, housing);
        return sql.toString();

    }

    /**
     * 查询物业用房列表
     * @param map 查询实体
     * @return 列表
     */
    public String queryHousingList(HashMap<String, Object> map) {
        StringBuffer sql = new StringBuffer(20);
        Housing housing = (Housing) map.get("housing");
        Page page = (Page) map.get("page");
        sql.append("SELECT id,project_id as projectId,room_address as roomAddress,room_nature as roomNature,room_area as roomArea,");
        sql.append("room_state as roomState,remark,room_all_area as roomAllArea,other_area as otherArea,room_path as roomPath,protocol");
        sql.append(" FROM company_housing WHERE project_id=#{housing.projectId}");
        commonSql(sql, housing);
        sql.append(" LIMIT ");
        sql.append(page.getBeginIndex()).append(",").append(page.getLimit());
        return sql.toString();

    }

    private void commonSql(StringBuffer sql, Housing housing) {
        if (StringUtils.isNotBlank(housing.getRoomAddress())) {
            sql.append(" AND INSTR(room_address,#{housing.roomAddress})>0");
        }
        if (housing.getRoomNature()!=null) {
            sql.append(" AND room_nature=#{housing.roomNature}");
        }
    }
}
